<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax 发送 GET 请求</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <button class="btn">点我接收</button>
    <div class="box"></div>
</body>
<script>
    var btn = document.querySelector(".btn")
    var box = document.querySelector(".box")

    btn.addEventListener('click',function(){
        // console.log('test');
        //1.创建对象
        var xhr = new XMLHttpRequest();
        //2.初始化 设置请求方法和url
        //传参方式:url?name=value&name=value&name=value...
        xhr.open('GET','http://127.0.0.1:8000/server?a=100&b=200&c=300')
        //3.发送
        xhr.send();
        //4.事件绑定 处理服务端返回结果
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(200 === xhr.status){
                    console.log(xhr.status);//状态码
                    console.log(xhr.statusText);//状态字符串
                    console.log(xhr.getAllResponseHeaders());//所有响应头
                    console.log(xhr.response);//响应体

                    box.innerHTML = xhr.response

                }else{
                    console.log(xhr.status);
                }
            }else{
                console.log('错误'+xhr.readyState);
            }
        }
    })
</script>
</html>